package br.com.srv.componentes.panico.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import HTTPClient.HTTPConnection;
import br.com.srv.dao.WebServiceBaseDAO;
import br.com.srv.model.ComandoTO;
import br.com.srv.model.ConfiguracaoTO;
import br.com.srv.util.ArenaHelper;
import br.com.srv.util.Util;

public class PanicoAlarmeDAOImpl extends WebServiceBaseDAO implements
		PanicoAlarmeDAO {
	
	public PanicoAlarmeDAOImpl(HTTPConnection connection) {
		setHttpConnection(connection);
	}

	public List<ConfiguracaoTO> buscarTelefonesPanicoAlarme(String veiculoId)
			throws Exception {		
		
		List<Map> maps = consultarComandos(veiculoId);
		
		List<ConfiguracaoTO> configuracoes = new ArrayList<ConfiguracaoTO>();
		
		for (Map map : maps) {
			String param = map.get("PARAM").toString();
			if (Util.isCommand(ComandoTO.COMANDO_CONFIGURAR, param)) {
				ConfiguracaoTO configuracaoTO = new ConfiguracaoTO();
								
				int beginIndex = param.indexOf(':')+1;
				int endIndex = param.indexOf('[');
				String[] telefones = param.substring(
						beginIndex, endIndex).split(",");
				
				configuracaoTO.setDataCadastro((String)map.get("DATE_CAD"));
				try {
					if (telefones[0] != null && !telefones[0].equals("")) {
						configuracaoTO.setTelefone1(telefones[0]);
					}
					if (telefones[1] != null && !telefones[1].equals("")) {
						configuracaoTO.setTelefone2(telefones[1]);
					}
					if (telefones[2] != null && !telefones[2].equals("")) {
						configuracaoTO.setTelefone3(telefones[2]);
					}
					if (telefones[3] != null && !telefones[3].equals("")) {
						configuracaoTO.setTelefone4(telefones[3]);
					}
				} catch(Exception e) {
					
				}
				configuracoes.add(configuracaoTO);
			}
		}
		
		return configuracoes;
	}

	public boolean cadastrarConfiguracaoPanicoAlarme(String veiculoId,
			ConfiguracaoTO configuracaoTO) throws Exception {
		String telefones = configuracaoTO.getTelefone1();
		telefones += "," + configuracaoTO.getTelefone2();
		telefones += "," + configuracaoTO.getTelefone3();
		telefones += "," + configuracaoTO.getTelefone4()+",";
		
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER);
		urlString.append("<COMMAND_DATA_TERMINAL>");
		urlString.append("<SERIAL>1</SERIAL>");
		urlString.append("<TEXT>" + ComandoTO.COMANDO_CONFIGURAR + telefones + "[</TEXT>");
		urlString.append("<ATTEMPTS>1</ATTEMPTS>");
		urlString.append("<VEHICLES>");
		urlString.append("<VEHICLE_ID>" + veiculoId + "</VEHICLE_ID>");
		urlString.append("</VEHICLES>");
		urlString.append("</COMMAND_DATA_TERMINAL>");	
		
		String xml = queryArenaMethod(
				ArenaHelper.WEB_SERVICE_MTCDATATERMINALFREETEXT, urlString);
		
		isResultaoOk(xml);
		
		return true;
	}
	
	private void isResultaoOk(String xml) throws Exception {
		if (!xml.equals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><RESULT>OK</RESULT>")) {
			System.out.println("[ERRO:] " + xml);
			throw new Exception();
		}
	}
	
	@SuppressWarnings("unchecked")
	private List<Map> consultarComandos(String veiculoId) throws Exception {
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER);
		urlString.append("<FILTER>");
		urlString.append("<DATE_TIME_START>2005-01-01 00:00:00</DATE_TIME_START>");
		urlString.append("<DATE_TIME_STOP>2010-01-01 23:59:59</DATE_TIME_STOP>");
		urlString.append("<VEHICLES>");
		urlString.append("<VEHICLE_ID>" + veiculoId + "</VEHICLE_ID>");
		urlString.append("</VEHICLES>");
		urlString.append("<COMMAND_TYPES>");
		urlString.append("<ID>261</ID>");
		urlString.append("</COMMAND_TYPES>");
		urlString.append("<LIMIT>");
		urlString.append("<COUNT>0</COUNT>");
		urlString.append("<QUANT>0</QUANT>");
		urlString.append("<OFFSET>0</OFFSET>");
		urlString.append("</LIMIT>");
		urlString.append("<SHOW_FIELDS>");
		urlString.append("<VEHICLE_ID>1</VEHICLE_ID>");
		urlString.append("<VEHICLE_COD>1</VEHICLE_COD>");
		urlString.append("<VEHICLE_DESC>1</VEHICLE_DESC>");
		urlString.append("<DATE_CAD>1</DATE_CAD>");
		urlString.append("<DATE_EXE>1</DATE_EXE>");
		urlString.append("<PARAM>1</PARAM>");
		urlString.append("</SHOW_FIELDS>");
		urlString.append("</FILTER>");
		
		String xml = queryArenaMethod(ArenaHelper.WEB_SERVICE_REPORTCOMMANDSENT, urlString);

		List<Map> maps = parseList(xml, "/COMMAND_SENT/VEHICLE", "COMMAND");		
		
		return maps;
	}	

}
